package com.ctl.jvm;

import com.ctl.jvm.model.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class ThreadLocalTest {
    static class Consumer extends Thread {
        ThreadLocal<Integer> threadLocal;

        public Consumer(ThreadLocal<Integer> threadLocal) {
            this.threadLocal = threadLocal;
        }

        @Override
        public void run() {
            List<Long> list = new ArrayList<>();
            for (int i = 0; i < 9999999; i++) {
                list.add(System.currentTimeMillis());
                threadLocal.set(new Random().nextInt());
                if (i % 1000 == 0) {
                    try {
                        Thread.sleep(new Random().nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (threadLocal.get() == null) {
                    System.out.println(Thread.currentThread() + "\t" + threadLocal.get());
                }
                new Person(UUID.randomUUID().toString(),"ctl",30,(byte)0,"xx"+i,6000000);
            }
            try {
                Thread.sleep(1000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
        new Consumer(threadLocal).start();
//        new Consumer(threadLocal).start();
        Thread.sleep(100000);
    }
}
